################
#PSRM: Explaining Support for Redistribution: Social Insurance Systems and Fairness
#
#Observational Data
#Part V: Recoding
#
#Verena Fetscher
#July 2022
####################

rm(list=ls())

##########################
#Load Data
##########################

load("DataFile_05_Risk.Rda")



##########################
#Generate variables
##########################

data$single<-0
data$single[(data$marital=="Never married"&data$chldhhe=="No")|
              (data$maritala=="Never married and never in civil partnership"&
                 data$chldhhe=="No")|
              (data$maritalb=="None of these (NEVER married or in legally registered civil"&
                 data$chldhhe=="No")]<-1
table(data$single)

data$married67<-0
data$married67[(data$marital=="Married"&data$chldhhe=="Yes")|
                 (data$maritala=="Married"&data$chldhhe=="Yes")|
                 (data$maritalb=="Legally married"&data$chldhhe=="Yes")]<-1
table(data$married67)


##########################
#Import inequality measure
##########################

data$cntryName[data$cntry=="AT"]<-"Austria"
data$cntryName[data$cntry=="BE"]<-"Belgium"
data$cntryName[data$cntry=="CH"]<-"Switzerland"
data$cntryName[data$cntry=="DE"]<-"Germany"
data$cntryName[data$cntry=="DK"]<-"Denmark"
data$cntryName[data$cntry=="ES"]<-"Spain"
data$cntryName[data$cntry=="FI"]<-"Finland"
data$cntryName[data$cntry=="FR"]<-"France"
data$cntryName[data$cntry=="GB"]<-"United Kingdom"
data$cntryName[data$cntry=="IE"]<-"Ireland"
data$cntryName[data$cntry=="IT"]<-"Italy"
data$cntryName[data$cntry=="NL"]<-"Netherlands"
data$cntryName[data$cntry=="NO"]<-"Norway"
data$cntryName[data$cntry=="PT"]<-"Portugal"
data$cntryName[data$cntry=="SE"]<-"Sweden"

data$year[data$essround==1]<-2002
data$year[data$essround==2]<-2004
data$year[data$essround==3]<-2006
data$year[data$essround==4]<-2008
data$year[data$essround==5]<-2010
data$year[data$essround==6]<-2012
data$year[data$essround==7]<-2014


#Standardized World Income Inequality Database
#https://dataverse.harvard.edu/dataset.xhtml?persistentId=hdl:1902.1/11992
#February 2018
load("swiid6_1.Rda")

summary(swiid_summary$gini_disp)


dnew<-data.frame(swiid_summary$gini_disp,swiid_summary$country,swiid_summary$year)

dnew %>%
  group_by(swiid_summary.country) %>% 
  filter (! duplicated(swiid_summary.year))

names(dnew)<-c("gini.disp","country","year")
dnew$cntryName<-as.character(dnew$country)

dnew<-dnew[dnew$year==2002|dnew$year==2004|dnew$year==2006|dnew$year==2008|
             dnew$year==2010|dnew$year==2012|dnew$year==2014,]

table(data$cntryName)
table(dnew$cntryName)
dnew<-dnew[(dnew$cntryName %in% data$cntryName),]

data <- left_join(data,dnew)

rm(dnew)
rm(swiid)
rm(swiid_summary)


##########################
#Recode variables
##########################


#Redistribution
table(data$gincdif)

data$redistribution_num<-as.numeric(data$gincdif)

data$redistribution_num[data$redistribution_num>5]<-NA

table(data$redistribution_num)

# Referse answer categories
data$redistribution<-NA
data$redistribution[data$redistribution_num==1]<-5
data$redistribution[data$redistribution_num==2]<-4
data$redistribution[data$redistribution_num==3]<-3
data$redistribution[data$redistribution_num==4]<-2
data$redistribution[data$redistribution_num==5]<-1

table(data$redistribution)  
##########################


#Social identity
#same culture and traditions important
table(data$pplstrd)
data$identity<-NA
data$identity[data$pplstrd=="Agree strongly"]<-5
data$identity[data$pplstrd=="Agree"]<-4
data$identity[data$pplstrd=="Neither agree nor disagree"]<-3
data$identity[data$pplstrd=="Disagree"]<-2
data$identity[data$pplstrd=="Disagree strongly"]<-1

table(data$identity)
##########################


#Income to thousands
data$income.PPP.th<-data$income.PPP/1000
data$income.dist.th<-data$income_dist/1000

summary(data$income.PPP[data$cntry=="FR"])
##########################

#Female
data$gender<-NA
data$gender[data$gndr=="Female"]<-1
data$gender[data$gndr=="Male"]<-0
table(data$gender)
##########################


#Unemployment
#Using this card, which of these descriptions applies to what you 
#have been doing for the last 7 days?
#Unemployed and actively looking for a job
data$uempl<-NA
data$uempl[data$uempla=="Marked"]<-1
data$uempl[data$uempla=="Not marked"]<-0
table(data$uempl)
##########################


#Standardize variables
#range <- function(x){(x-min(x,na.rm=T))/(max(x,na.rm=T)-min(x,na.rm=T))}
##########################


##########################
#Save data
##########################

save(data,file="DataFile_06_combined.Rda")


